package sp.whereru;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NavigationActivity extends Activity implements SensorEventListener {
	private static SensorManager sensorManager;
	private NavigationView navView;
	private GPS gps;

	private float[] magneticFields;
	private float[] acceleration;
	private float[] orientation;
	private float[] rotationMatrix;
	private float[] remapped;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nav_tool);
		gps = new GPS(this);
		gps.startGPS();
		gps.getCoordinates();		
		gps.stopGPS();
		final Button gpsButton = (Button) findViewById(R.id.gpsButton);
		gpsButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				gps.startGPS();
				gps.getCoordinates();		
				gps.stopGPS();     	 
			}
		});
		final Button closeButton = (Button) findViewById(R.id.closeButton);
		closeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();      	 
			}
		}); 	  
		navView = (NavigationView)findViewById(R.id.navView);	  
		sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		registerListeners();
	}

	private void registerListeners() {
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
	}

	private void unregisterListeners() {
		sensorManager.unregisterListener(this);
	}

	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			acceleration = new float[3];
			System.arraycopy(event.values, 0, acceleration, 0, 3);	        	
		}
		if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
			magneticFields = new float[3];
			System.arraycopy(event.values, 0, magneticFields, 0, 3);
		}
		if(acceleration != null && magneticFields != null){
			rotationMatrix = new float[9];
			if (SensorManager.getRotationMatrix(rotationMatrix, null, magneticFields, acceleration)) {
				remapped = new float[9];
				if(SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, remapped)){				
					orientation = new float[3];
					SensorManager.getOrientation(rotationMatrix, orientation);
					navView.updateDirection((float)(orientation[0]), gps);
				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		unregisterListeners();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		registerListeners();
		super.onResume();	
	}

	@Override
	protected void onPause() {
		unregisterListeners();
		super.onPause();
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub		
	}
}